package cl.ceis.demo2;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

public class LienzoGL extends GLSurfaceView {
	
	private LienzoRenderer renderer;

	public LienzoGL(Context context, AttributeSet attrs) {
		super(context, attrs);	

	}

	public LienzoGL(Context context) {
		super(context);
		renderer = new LienzoRenderer();
		setRenderer(renderer);	
	}

	@Override
	public boolean onTouchEvent(final MotionEvent event) {		
		return true;
	}
	
	

}

class LienzoRenderer implements GLSurfaceView.Renderer{

	private FloatBuffer triangleVB;
	
	private void initShapes(){
	    
        float triangleCoords[] = {
            // X, Y, Z
            -0.5f, -0.25f, 0,
             0.5f, -0.25f, 0,
             0.0f,  0.559016994f, 0
        }; 
        
//        Log.i("coordenadas_triangulo", String.valueOf(triangleCoords));
        
        // initialize vertex Buffer for triangle  
        ByteBuffer vbb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
                triangleCoords.length * 4); 
        vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
        triangleVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
        triangleVB.put(triangleCoords);    // add the coordinates to the FloatBuffer
        triangleVB.position(0);            // set the buffer to read the first coordinate
    
    }
	
	

	public void onDrawFrame(GL10 gl) {
		
		// Draw the triangle
        gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
//        Log.i("vertex", String.valueOf(triangleVB.get(0)));
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
        gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, 3);
		
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background frame color
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        
        // initialize the triangle vertex array
        initShapes();
        
        // Enable use of vertex arrays
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        
        
		
		
		
	}
	
}
